﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

public partial class system_sjgl_sjk : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PageData.Paging("wtpaper", "select distinct papername,papertype,sinoname from wtpaper", LabCount, LabPageCount, LabCurPage, HyLFirPage, HyLPrePage, HyLNextPage, HyLastPage, Repeater1, 30);
    
            AspNetPager1.RecordCount = SQL.ExecScalar("select count(*) from wtproblem");
            AspNetPager1.PageSize = 60;

            PageData.bindData(RepTJ, "select pkID,wtproname,wtprotype from wtproblem group by pkID,wtproname,wtprotype order by pkID desc", "wtproblem", AspNetPager1);

            Aspnetpager2.RecordCount = SQL.ExecScalar("select count(*) from wtproblem");
            Aspnetpager2.PageSize = 60;

            PageData.bindData(RepTJ, "select pkID,wtproname,wtprotype from wtproblem group by pkID,wtproname,wtprotype order by pkID desc", "wtproblem", Aspnetpager2);


            DropDownList1.DataTextField = "sinoname";
            DropDownList1.DataValueField = "sinoname";
            DropDownList1.DataSource = SQL.GetDataTable("sinopro", "select sinoname from sinopro");
            DropDownList1.DataBind();

            ArrayList mList = new ArrayList();
            mList.Add("简单");
            mList.Add("中等");
            mList.Add("困难");
            BindDropDownList.SetDropDownList(mList,DropDownList2,"请选择试卷难度");
        }
    }
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        PageData.bindData(RepTJ, "select pkID,wtproname,wtprotype from wtproblem group by pkID,wtproname,wtprotype order by pkID desc", "wtproblem", AspNetPager1);
    }
    protected void AspNetPager2_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        Aspnetpager2.CurrentPageIndex = e.NewPageIndex;
        PageData.bindData(RepTJ, "select pkID,wtproname,wtprotype from wtproblem group by pkID,wtproname,wtprotype order by pkID desc", "wtproblem", Aspnetpager2);
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        //全选
        CheckBox2.Checked = true;
        CheckBox3.Checked = false;
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
            if (!CheckBox1.Checked)
            {
                CheckBox1.Checked = true;
            }
        }
    }
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {
        //反选
        CheckBox3.Checked = true;
        CheckBox2.Checked = false;
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
            if (CheckBox1.Checked)
            {
                CheckBox1.Checked = false;
            }
            else
            {
                CheckBox1.Checked = true;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //删除
        bool panduan = false;
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
            if (CheckBox1.Checked)
            {
                Label Label1 = (Label)item.FindControl("Label1");
                string papername = Label1.Text;
                try
                {
                    SQL.ExecScalarSQL("delete from wtpaper where papername='" + papername + "'");
                }
                catch
                {
                    panduan = true;
                    break;
                }
            }
        }
        if (!panduan)
        {
            MagicAjax.AjaxCallHelper.WriteAlert("删除成功！");
            PageData.Paging("wtpaper", "select distinct papername,papertype,sinoname from wtpaper", LabCount, LabPageCount, LabCurPage, HyLFirPage, HyLPrePage, HyLNextPage, HyLastPage, Repeater1, 16);
        }
        else
        {
            MagicAjax.AjaxCallHelper.WriteAlert("系统出现错误！");
        }
    }
    protected void addShiJuan(object sender, EventArgs e)
    {
        //本系统的操作为首先在每页选当前页的想要添加的题目，系统记录下该操作，当所添加的题目达到要求时，button按钮进行自动变换，将试题入库
        if (LabNumber.Text == "true")
        {
            //试卷组合完成，可以添加入库
            Hashtable mHsTable = new Hashtable();
            List<string> SQLStringList = new List<string>();
            //将session转化为Hashtable，获得已经添加的试题
            if (Session["mHsPromble"] != null)
            {
                mHsTable = (Hashtable)Session["mHsPromble"];
            }
            else
            {
                //为空就结束操作
                return;
            }
            foreach (DictionaryEntry dEntry in mHsTable)
            {
                //遍历哈希表，组织一个插入事务，将试卷按题目入库
                StringBuilder mSBuilber = new StringBuilder();
                mSBuilber.Append("insert into wtpaper(papername,papertype,problemID,wtdifficulty,sinoname,problemType) values(");
                mSBuilber.Append("'" + TextBox1.Text.Trim() + "',");
                mSBuilber.Append("'正式卷',");
                mSBuilber.Append(""+int.Parse(dEntry.Key.ToString())+",");
                mSBuilber.Append("'" + DropDownList2.SelectedItem.Text + "',");
                mSBuilber.Append("'" + DropDownList1.SelectedItem.Text + "',");
                mSBuilber.Append("'" + dEntry.Value.ToString() + "')");
                SQLStringList.Add(mSBuilber.ToString());
            }
            if (SQL.ExecuteSqlTran(SQLStringList))
            {
                MagicAjax.AjaxCallHelper.WriteAlert("试卷添加成功！");
                CookieOper.CleanSession("mHsPromble");
            }
        }
        else
        {
            //继续往试卷内添加题目
            Hashtable mHs = new Hashtable();
            if (Session["mHsPromble"] != null)
            {
                mHs = (Hashtable)Session["mHsPromble"];
            }
            bool TotalIsHave = false;
            for (int i = 0; i < RepTJ.Items.Count; i++)
            {
                //计算选择题
                RepeaterItem reItem = (RepeaterItem)RepTJ.Items[i];
                CheckBox CheckBoxTJ = (CheckBox)reItem.FindControl("CheckBoxTJ");
                Label LabelTJ = (Label)reItem.FindControl("LabelTJ");//记录选择pkID
                Label Label2 = (Label)reItem.FindControl("Label2");//记录选择题目类型
                //string str = "";
                if (CheckBoxTJ.Checked)
                {
                    //先比较用户有没有添加同样的试题，将重复添加的试题前面的勾去掉，并结束操作
                    bool IsHaveValue = false;
                    foreach (DictionaryEntry dEntry in mHs)
                    {
                        if (LabelTJ.Text == dEntry.Key.ToString())
                        {
                            IsHaveValue = true;//添加同样的试题，将IsHaveValue=true
                            break;
                        }
                    }
                    if (IsHaveValue)
                    {
                        TotalIsHave = true;
                        CheckBoxTJ.Checked = false;//添加同样的试题，将重复添加的试题前面的勾去掉
                        continue;
                    }
                    mHs.Add(LabelTJ.Text, Label2.Text);
                }
            }
            if (TotalIsHave)
            {
                MagicAjax.AjaxCallHelper.WriteAlert("请勿添加相同试题，系统已经将同样的试题删除！");
                return;//结束操作
            }
            Session["mHsPromble"] = mHs;

            //显示试题在该卷中的分布情况,遍历hash表
            int SelectNumber = 0;//选择题得数量
            int PanDuNumber = 0;//判断题得数量

            foreach (DictionaryEntry dEntry in mHs)
            {
                //做一个用户交互，提醒用户试题的添加情况
                 string TiMuType = SQL.GetData("select wtprotype from wtproblem where pkID=" + int.Parse(dEntry.Key.ToString()) + "", "wtprotype");
                if (TiMuType.Equals("选择题"))
                {
                    SelectNumber = SelectNumber + 1;
                }
                else
                {
                    PanDuNumber = PanDuNumber + 1;
                }
            }
            LbTiShi.Text = "已经选择" + SelectNumber + "道选择题，" + PanDuNumber + "道判断题。";
            if ((SelectNumber + PanDuNumber) < 60)
            {
                LabNumber.Text = "false";//标记，用户进行button的变换
            }
            else if ((SelectNumber + PanDuNumber)==60)
            {
                LabNumber.Text = "true";//标记，用户进行button的变换
                Button1.Text = "添加入库";///button的变换
            }
        }
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.RepTJ.Items)
        {
            CheckBox CheckBoxTJ = (CheckBox)item.FindControl("CheckBoxTJ");
            if (!CheckBoxTJ.Checked)
            {
                CheckBoxTJ.Checked = true;
            }
        }
    }
}
